//
//  EnvironmentEditTableCell.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/9.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "EnvironmentEditTableCell.h"
@interface EnvironmentEditTableCell()<UITextFieldDelegate>

@end
@implementation EnvironmentEditTableCell

- (void)initView{
    [super initView];
     _line1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, .5)];
    [self addSubview:_line1];
    _nameLab=[[UILabel alloc]init]; //WithFrame:CGRectMake(15, 8, 36, 24)];
    _nameLab.textColor=C(@"585858");
    _nameLab.font=F(15);
    [self addSubview:_nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self).offset(8);
        make.height.equalTo(@(24));
        
    }];

    _contexLab=[[UILabel alloc]init];//WithFrame:CGRectMake(CGRectGetMaxX(_nameLab.frame)+10, 8, WindowWidth-CGRectGetMaxX(_nameLab.frame)-10-15, 24)];
    [self addSubview:_contexLab];
    [self.contexLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self.nameLab.mas_bottom).offset(5);
        make.right.equalTo(self).offset(-15);
        
    }];
    _contexLab.textAlignment=NSTextAlignmentLeft;
    _contexLab.textColor=C(@"c2c2c2");
    _contexLab.font=F(11);
    _contexLab.numberOfLines=0;
 
    
    UIImage *img=[UIImage imageNamed:@"Ico_input_NoSelect"];
    
    _selectView=[[SelectPollingView alloc]init];
    [self addSubview:_selectView];
    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(0);
        make.top.equalTo(self.contexLab.mas_bottom).offset(10);
        make.right.equalTo(self).offset(-15);
        make.height.equalTo(@(img.size.height));
    }];
    _textBgView=[[UIView alloc]init];
    _textBgView.backgroundColor=C(@"f5f4f4");
    _textBgView.layer.borderColor=C(@"cdcdcd").CGColor;
    _textBgView.layer.borderWidth=0.5;
    [self addSubview:_textBgView];
    [self.textBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.top.equalTo(self.selectView.mas_bottom).offset(18);
        make.right.equalTo(self).offset(-48);
        make.height.equalTo(@(30));
    }];
     _textField=[[UITextField alloc]initWithFrame:CGRectMake(5, 0, WindowWidth-30-18-5, 30)];
    _textField.textColor=C(@"585858");
    _textField.font=F(12);
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification"
                                              object:_textField];
    [_textBgView addSubview:_textField];
    
    _startLab=[[UILabel alloc]init];
    _startLab.font=F(14);
    _startLab.textColor=MainColor;
    [self addSubview:_startLab];
    [self.startLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.textBgView.mas_right).offset(5);
        make.top.equalTo(self.selectView.mas_bottom).offset(18);
        make.width.equalTo(@(18));
        make.height.equalTo(@(30));
    }];

    _startLab.text=@"*";
    
    _line2=[[UILabel alloc]init];    [self addSubview:_line2];
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.height.equalTo(@(.5));
        make.bottom.equalTo(self);
        
    }];

    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
}

-(void)textFiledEditChanged:(NSNotification *)obj{
    UITextField *textField = (UITextField *)obj.object;
    
    NSString *toBeString = textField.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > 20) {
                textField.text = [toBeString substringToIndex:20];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > 20) {
            textField.text = [toBeString substringToIndex:20];
        }
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:@"UITextFieldTextDidChangeNotification"
                                                 object:_textField];
}

@end
